// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"server/components/models/ent/predicate"
	"server/components/models/ent/regiontype"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// RegionTypeUpdate is the builder for updating RegionType entities.
type RegionTypeUpdate struct {
	config
	hooks    []Hook
	mutation *RegionTypeMutation
}

// Where appends a list predicates to the RegionTypeUpdate builder.
func (rtu *RegionTypeUpdate) Where(ps ...predicate.RegionType) *RegionTypeUpdate {
	rtu.mutation.Where(ps...)
	return rtu
}

// SetType sets the "type" field.
func (rtu *RegionTypeUpdate) SetType(s string) *RegionTypeUpdate {
	rtu.mutation.SetType(s)
	return rtu
}

// SetNillableType sets the "type" field if the given value is not nil.
func (rtu *RegionTypeUpdate) SetNillableType(s *string) *RegionTypeUpdate {
	if s != nil {
		rtu.SetType(*s)
	}
	return rtu
}

// Mutation returns the RegionTypeMutation object of the builder.
func (rtu *RegionTypeUpdate) Mutation() *RegionTypeMutation {
	return rtu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (rtu *RegionTypeUpdate) Save(ctx context.Context) (int, error) {
	return withHooks(ctx, rtu.sqlSave, rtu.mutation, rtu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (rtu *RegionTypeUpdate) SaveX(ctx context.Context) int {
	affected, err := rtu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (rtu *RegionTypeUpdate) Exec(ctx context.Context) error {
	_, err := rtu.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (rtu *RegionTypeUpdate) ExecX(ctx context.Context) {
	if err := rtu.Exec(ctx); err != nil {
		panic(err)
	}
}

func (rtu *RegionTypeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(regiontype.Table, regiontype.Columns, sqlgraph.NewFieldSpec(regiontype.FieldID, field.TypeInt64))
	if ps := rtu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := rtu.mutation.GetType(); ok {
		_spec.SetField(regiontype.FieldType, field.TypeString, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, rtu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{regiontype.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	rtu.mutation.done = true
	return n, nil
}

// RegionTypeUpdateOne is the builder for updating a single RegionType entity.
type RegionTypeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *RegionTypeMutation
}

// SetType sets the "type" field.
func (rtuo *RegionTypeUpdateOne) SetType(s string) *RegionTypeUpdateOne {
	rtuo.mutation.SetType(s)
	return rtuo
}

// SetNillableType sets the "type" field if the given value is not nil.
func (rtuo *RegionTypeUpdateOne) SetNillableType(s *string) *RegionTypeUpdateOne {
	if s != nil {
		rtuo.SetType(*s)
	}
	return rtuo
}

// Mutation returns the RegionTypeMutation object of the builder.
func (rtuo *RegionTypeUpdateOne) Mutation() *RegionTypeMutation {
	return rtuo.mutation
}

// Where appends a list predicates to the RegionTypeUpdate builder.
func (rtuo *RegionTypeUpdateOne) Where(ps ...predicate.RegionType) *RegionTypeUpdateOne {
	rtuo.mutation.Where(ps...)
	return rtuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (rtuo *RegionTypeUpdateOne) Select(field string, fields ...string) *RegionTypeUpdateOne {
	rtuo.fields = append([]string{field}, fields...)
	return rtuo
}

// Save executes the query and returns the updated RegionType entity.
func (rtuo *RegionTypeUpdateOne) Save(ctx context.Context) (*RegionType, error) {
	return withHooks(ctx, rtuo.sqlSave, rtuo.mutation, rtuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (rtuo *RegionTypeUpdateOne) SaveX(ctx context.Context) *RegionType {
	node, err := rtuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (rtuo *RegionTypeUpdateOne) Exec(ctx context.Context) error {
	_, err := rtuo.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (rtuo *RegionTypeUpdateOne) ExecX(ctx context.Context) {
	if err := rtuo.Exec(ctx); err != nil {
		panic(err)
	}
}

func (rtuo *RegionTypeUpdateOne) sqlSave(ctx context.Context) (_node *RegionType, err error) {
	_spec := sqlgraph.NewUpdateSpec(regiontype.Table, regiontype.Columns, sqlgraph.NewFieldSpec(regiontype.FieldID, field.TypeInt64))
	id, ok := rtuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "RegionType.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := rtuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, regiontype.FieldID)
		for _, f := range fields {
			if !regiontype.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != regiontype.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := rtuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := rtuo.mutation.GetType(); ok {
		_spec.SetField(regiontype.FieldType, field.TypeString, value)
	}
	_node = &RegionType{config: rtuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, rtuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{regiontype.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	rtuo.mutation.done = true
	return _node, nil
}
